
import 'dart:io';

import 'package:baselib/utils/gallery_camera_pick_util.dart';
import 'package:baselib/utils/keyboard_util.dart';
import 'package:baselib/utils/load_util.dart';
import 'package:baselib/utils/log_util.dart';
import 'package:baselib/utils/navigator_util.dart';
import 'package:baselib/utils/toast_util.dart';
import 'package:baselib/widgets/comm_message_gradient_dialog.dart';
import 'package:baselib/widgets/refresh_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/editable_text.dart';
import 'package:framelib/base/base_view_model.dart';
import 'package:framelib/config/api_config.dart';
import 'package:framelib/config/route_config.dart';
import 'package:framelib/net/baseentity.dart';
import 'package:framelib/utils/drawable_util.dart';
import 'package:framelib/utils/nim_sdk_util.dart';
import 'package:framelib/utils/oss_util.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:framelib/widgets/page_widget.dart';
import 'package:get/state_manager.dart';
import 'package:get/get.dart';
import 'package:module_business/module_comm/model/interest_tag_entity.dart';
import 'package:module_business/module_comm/model/user_params_model.dart';
import 'package:module_business/module_comm/service/module_comm_service.dart';
import 'package:module_business/module_index/model/index_room_item_entity.dart';
import 'package:module_business/module_index/model/voice_person_entity.dart';
import 'package:module_business/module_index/service/module_index_service.dart';
import 'package:nim_core/nim_core.dart';

import 'index_view_model.dart';
import 'my_room_view_model.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';


class RoomBlocklistViewModel extends BaseViewModel<ModuleIndexService>{

  ModuleIndexService moduleIndexService;
  IndexRoomItemEntity roomInfo;
  NIMChatroomInfo roomYxInfo;
  RefreshController refreshController = RefreshController(initialRefresh: true);
  RefreshState loadStatus=RefreshState.OK;
  int page = 1;
  bool isCanMoreData = false;
  List<VoicePersonEntity> chatRoomBlocklist = [];
  //
  VoicePersonEntity voicePersonEntity = VoicePersonEntity();

  @override
  ModuleIndexService createService() =>moduleIndexService = ModuleIndexService();
  @override
  void onInit() {
    super.onInit();
    Map<String,dynamic> arguments = Get.arguments??{};
    roomInfo =  arguments['roomInfo'];
    roomYxInfo =  arguments['roomYxInfo'];
    //查询此用户和此房间的关系
    queryUserRoomRelationType();
  }

  @override
  void onClose() {
    super.onClose();
  }
  onRefresh({int pageCur,}) async{
    if(pageCur!=null)page=pageCur;
    Map<String,dynamic> upData={
      'searchValue':roomInfo?.id,
    };
    BaseEntity baseEntity= await moduleIndexService
        .api_voicechat_room_queryRoomBlackUser(parmsMap: upData);
    if(refreshController!=null)refreshController.refreshCompleted();
    if(refreshController!=null)refreshController.loadComplete();
    if(baseEntity?.code==ApiConfig.SUCCESS_CODE){
      List<VoicePersonEntity> dataTemp=baseEntity?.result??[];
      if(page==1){
        chatRoomBlocklist=dataTemp;
      }else{
        chatRoomBlocklist.addAll(dataTemp);
      }
      if(((dataTemp?.length??0)<10)){
        isCanMoreData=false;
      }else{
        isCanMoreData=true;
      }
      //计算是否为空
      if(((chatRoomBlocklist?.length??0)==0)){
        loadStatus=RefreshState.EMPTY;
      }else{
        //计算被选中的
        loadStatus=RefreshState.OK;
      }
    }else{
      loadStatus=RefreshState.NET_ERROR;
      ToastUtil.toast(baseEntity?.message);
    }
    update();
  }
  //弹出解绑
  unBindAdmin(int index) {
    showDialog(
        context: Get.context,
        builder: (_){
          return CommMessageGradientDialog(
            message: '${StringUtil.instance.Revoke} ${chatRoomBlocklist[index]?.nickname} ${StringUtil.instance.from_the_blocklist}?',
            onPositivePressEvent: (){
              LogUtil.d('showDialog-->onPositivePressEvent');
              Navigator.of(Get.context).pop();
              //移除，并且只有创建者可以对管理员进行操作，管理员不能对创建者和其他管理员进行操作。
              toRevoke(index);
            },
            onCloseEvent: (){
              LogUtil.d('showDialog-->onCloseEvent');
              Navigator.of(Get.context).pop();
            },
          );
        }
    );
  }
  //去移除，并且只有创建者可以对管理员进行操作，管理员不能对创建者和其他管理员进行操作。
  toRevoke(index) async{
    var dataItem = chatRoomBlocklist[index];
    LoadUtil.showLoad(loadingText: '${StringUtil.instance.unbinding}...');
    //关系类型:2:管理员,3:主播,4:黑名单,6:禁言
    Map<String,dynamic> upData={
      "relationType": 4,
      "roomId": roomInfo?.id,
      "userId": dataItem?.userId
    };
    LogUtil.d('toMyRoom--->upData=$upData');
    LoadUtil.hideLoad();
    BaseEntity baseEntity = await moduleIndexService.
    api_voicechat_room_bindOrUnBindUserRoomRelation(parmsMap: upData,isBind: false);
    if(baseEntity?.code == ApiConfig.SUCCESS_CODE){
      await NIMSDKUtil.instance.markChatroomMemberInBlackList(
          isAdd: false,
          roomId: roomYxInfo?.roomId,
          account: dataItem?.yxAccid
      );
      chatRoomBlocklist.removeAt(index);
      update();
      //刷新
      IndexViewModel indexViewModel = Get.find();
      if(indexViewModel!=null)indexViewModel.getMyRoom();
      MyRoomViewModel myRoomViewModel = Get.find();
      if(myRoomViewModel!=null)myRoomViewModel.getRommInfo();
    }else{
      ToastUtil.toast(baseEntity?.message);
    }
  }
  //查询此用户和此房间的关系
  void queryUserRoomRelationType() async{
    Map<String,dynamic> upData={
      "roomId": roomInfo?.id,
      "userId": UserParamsModel.instance.userId
    };
    BaseEntity baseEntity = await moduleIndexService.
    api_voicechat_room_queryUserRoomRelationType(parmsMap: upData);
    if(baseEntity?.code == ApiConfig.SUCCESS_CODE){
      voicePersonEntity = baseEntity?.result??VoicePersonEntity();
      update();
    }
  }
  //关系类型:2:管理员,3:主播,4:黑名单,6:禁言
  bool isHasPermission(){
    return (voicePersonEntity?.relationType == 1)||(voicePersonEntity?.relationType == 2);
  }
}
